import { ErrorReportConfig } from './types';
import { RequestTracker } from './utils/requestTracker';
import { JsError } from './modules/jsError';
import { LongTask } from './modules/longTask';
import { Timing } from './modules/timing';
import { PagePaint } from './modules/pagePaint';

const DEFAULT_CONFIG: Required<ErrorReportConfig> = {
    url: '', // 错误上报地址
    longTaskDuration: 3000 // 长任务阈值
}
export class Core {
    private config: Required<ErrorReportConfig>;
    private readonly requestTracker: RequestTracker;
    constructor(config: ErrorReportConfig) {
        this.config = {
            ...DEFAULT_CONFIG,
            ...config
        }
        this.validateConfig();
        this.requestTracker = new RequestTracker(this.config.url);
        new JsError(this.requestTracker);
        new LongTask(this.requestTracker, this.config.longTaskDuration);
        new Timing(this.requestTracker);
        new PagePaint(this.requestTracker);
    }

    private validateConfig(): void {
        if (!this.config.url) {
            throw new Error('url is required');
        }
    }
}

// 暴露全局API
if (typeof window !== 'undefined') {
    (window as any).HlErrorReportSDK = Core;
}
